<template>
  <div>
    <el-container>
      <!--头像-->
      <el-header>
        <router-link to="/admin">
          欢迎登录cilicili
          <!--https://cdn.jsdelivr.net/gh/yubifeng/blog-resource/bloghosting/website/static/websiteAvatar.webp-->
<!--
          <img alt="" class="mlogo" src="http://browser9.qhimg.com/bdm/800_600_100/t010448c46c1ecf7cab.jpg">
-->
        </router-link>
      </el-header>
      <!--主界面-->
      <el-main>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="demo-ruleForm" label-width="100px" status-icon>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" maxlength="12" type="text" label="描述文字"  placeholder="已禁用账号密码登录"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" autocomplete="off" type="password" placeholder="仅支持QQ登录"></el-input>
          </el-form-item>
          <el-form-item>
            <span><el-link href="/" class="btn btn-primary">点击，不登录访问</el-link></span>
            <el-button class="btn btn-primary" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <el-button type="primary"  @click="qqLoginClick" >QQ登录</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
      <!--回到顶部-->
      <el-backtop style="box-shadow: none;background: none;">
          <img alt="回到顶部" src="../assets/goLoginTop.png"
               style="margin-right: 70px;">
      </el-backtop>
  </div>

</template>

<script>
export default {
  name: 'Login',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: '',
        username: ''
      },
      rules: {
        password: [
          {validator: validatePass, trigger: 'blur'}
        ],
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur'}
        ]
      },
      qqParam:{

      }
    };
  },
    /*跳转QQ登录页面*/
  methods: {
    qqLoginClick() {
      this.$axios.post('/loginByQQ', this.ruleForm).then((res) => {
        //QQ登录的时候，将用于前后端通气的唯一表示Qstate,放到本地存起来，服务端存redis
        this.$store.commit('SET_QSTATE', res.data.data.state)
        //跳转QQ登录页面
        window.location.href = res.data.data.url+"&redirect_uri="+res.data.data.redirect_uri+"&client_id="+res.data.data.client_id+"&state="+res.data.data.state
      })

    },
    //登录
    submitForm(formName) {

      this.$alert("仅支持QQ扫码登录哦！", '提示', {
        confirmButtonText: '确定',
      });

      console.log(this.$store.getters.getQState);
      // console.log(this.$store.getters.getUser.username);

      const _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 提交逻辑
          this.$axios.post('/login', this.ruleForm).then((res) => {
            const token = res.headers['authorization']
            _this.$store.commit('SET_TOKEN', token)
            _this.$store.commit('SET_USERINFO', res.data.data)
            _this.$router.push("/admin")
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //清空用户名密码
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
  }
}
</script>

<style scoped>
.el-header, .el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}
.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 160px;
}
body > .el-container {
  margin-bottom: 40px;
}
.mlogo {
  height: 60%;
  margin-top: 10px;
}
.demo-ruleForm {
  max-width: 500px;
  margin: 0 auto;
}
</style>
